package com.lzb.watermarkcamera.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.watermarkcamera.R;
import com.lzb.watermarkcamera.adapter.WatermarkAdapter;
import com.lzb.watermarkcamera.basic.dialog.BaseSmartDialog;
import com.lzb.watermarkcamera.bean.WatermarkBean;
import com.lzb.watermarkcamera.databinding.DialogThemeBinding;
import com.lzb.watermarkcamera.databinding.DialogWatermarkTemplateBinding;
import com.lzb.watermarkcamera.utils.AppSpUtils;

import java.util.List;

public class WatermarkTemplateDialog extends BaseSmartDialog<DialogWatermarkTemplateBinding> implements View.OnClickListener {

    private int selPosition;
    private WatermarkAdapter watermarkAdapter;

    public WatermarkTemplateDialog(@NonNull Context context) {
        super(context);
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_watermark_template;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);

        mDataBinding.rvWatermarkList.setLayoutManager(new GridLayoutManager(getContext(), 2));
        watermarkAdapter = new WatermarkAdapter();
        mDataBinding.rvWatermarkList.setAdapter(watermarkAdapter);
        watermarkAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                watermarkAdapter.setSelPosition(position);
                if (listener != null) {
                    listener.clickItem(position);
                }
            }
        });
        watermarkAdapter.setSelPosition(selPosition);
        List<WatermarkBean> list = AppSpUtils.getWatermarkList();
        watermarkAdapter.setList(list);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem(int pos);
    }
}
